module generic_blk_mem #(
  parameter DEPTH=10
)(
  input clka,
  input [3:0] wea,
  input [DEPTH-1:0] addra,
  input [31:0] dina,
  output reg [31:0] douta,
  input clkb, // ignored, treated as clka
  input enb,
  input [3:0] web, // ignored, treated as 4'b0
  input [DEPTH-1:0] addrb,
  input [31:0] dinb, // ignored
  output reg [31:0] doutb
);

// Wrong endianness
//reg [31:0] m[0:2**DEPTH-1];
reg [0:31] m[0:2**DEPTH-1];

function [31:0] swapbytes;
  input [31:0] in;
  begin
    swapbytes = {
      in[ 7: 0],
      in[15: 8],
      in[23:16],
      in[31:24]
    };
  end
endfunction

always @(posedge clka) begin
  // read port A
  douta <= swapbytes(m[addra]);
  // write port A
  if (wea[0]) m[addra][ 0: 7] <= dina[ 7: 0];
  if (wea[1]) m[addra][ 8:15] <= dina[15: 8];
  if (wea[2]) m[addra][16:23] <= dina[23:16];
  if (wea[3]) m[addra][24:31] <= dina[31:24];
  // read port B
  if (enb) begin
    doutb <= swapbytes(m[addrb]);
  end
end

reg [639:0] memfile;
integer fd;
integer dummy;

initial begin
  if ($value$plusargs("memfile=%s", memfile)) begin
    fd = $fopen(memfile, "rb");
    dummy = $fread(m, fd);
  end else begin
    $display("WARNING: (binary) memfile not specified");
  end
end

endmodule
